﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_UpdateOrder : System.Web.UI.Page
{
    ShopDataContext db = new ShopDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["oId"] != null)
        {
            MultiView1.ActiveViewIndex = 1;
            if (!Page.IsPostBack)
            {
                var query = from o in db.Orders
                            join od in db.OrderDetails on o.oId equals od.oId
                            join p in db.Products on od.pId equals p.pId
                            where o.oOrderCode == Request.QueryString["oId"].ToString()
                            select new { OrderCode = o.oOrderCode, NameProduct = p.pName, Quantity = od.oQuantity };

                gvDetails2.DataSource = query;
                gvDetails2.DataBind();
            }
        }
        if (Request.QueryString["oId"] == null)
        {
            MultiView1.ActiveViewIndex = 0;

            if (!Page.IsPostBack)
            {
                var query = from o in db.Orders
                            select o;

                ddlOrders.DataSource = query;
                ddlOrders.DataValueField = "oOrderCode";
                ddlOrders.DataTextField = "oOrderCode";
                ddlOrders.DataBind();
            }
        }
    }

    protected void btnView_Click(object sender, EventArgs e)
    {
        if (ddlOrders.SelectedValue != null)
        {
            var query = from o in db.Orders
                        join od in db.OrderDetails on o.oId equals od.oId
                        join p in db.Products on od.pId equals p.pId
                        where o.oOrderCode == ddlOrders.SelectedValue
                        select new { OrderCode = o.oOrderCode, NameProduct = p.pName, Quantity = od.oQuantity };

            if (query.Count() > 0)
            {
                lblMessage.Text = "";
                gvDetails1.DataSource = query;
                gvDetails1.DataBind();
            }
            else
            {
                lblMessage.Text = "No data";
                gvDetails1.DataSource = null;
                gvDetails1.DataBind();
            }
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string orderCode = ddlOrders.SelectedItem.Value;

        Order order = db.Orders.FirstOrDefault(or => or.oOrderCode == orderCode);

        if (ddlStatus1.SelectedItem.Text != "Choose Status")
        {
            if (order != null)
            {
                order.oStatus = Convert.ToInt32(ddlStatus1.SelectedValue);

                List<int> qtys = new List<int>();

                foreach (GridViewRow row in gvDetails1.Rows)
                {
                    if(row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox tb = row.FindControl("txtQty") as TextBox;
                        if(tb != null)
                        {
                            qtys.Add(Convert.ToInt32(tb.Text));
                        }
                    }
                }

                int i = 0;
                foreach (OrderDetail od in order.OrderDetails)
                {
                    od.oQuantity = qtys[i];
                    i++;
                }

                db.SubmitChanges();

                Response.Redirect("ListOrders.aspx");
            }
        }
        else
        {
            lblMessage.Text = "Chooser Status";
        }
    }
    protected void btnUpdate2_Click(object sender, EventArgs e)
    {
        Order order = db.Orders.Single(or => or.oOrderCode == Request.QueryString["oId"].ToString());

        if (ddlStatus2.SelectedItem.Text != "Choose Status")
        {
            if (order != null)
            {
                order.oStatus = Convert.ToInt32(ddlStatus2.SelectedValue);

                List<int> qtys = new List<int>();

                foreach (GridViewRow row in gvDetails2.Rows)
                {
                    if (row.RowType == DataControlRowType.DataRow)
                    {
                        TextBox tb = row.FindControl("txtQty") as TextBox;
                        if (tb != null)
                        {
                            qtys.Add(Convert.ToInt32(tb.Text));
                        }
                    }
                }

                int i = 0;
                foreach (OrderDetail od in order.OrderDetails)
                {
                    od.oQuantity = qtys[i];
                    i++;
                }

                db.SubmitChanges();

                Response.Redirect("ListOrders.aspx");
            }
        }
        else
        {
            lblError.Text = "Chooser Status";
        }
    }
}